<template>
  <div class="home">
    <canvas ref="canvas" class="particles-canvas"></canvas>
    <div class="login-container">
      <div class="login-box">
        <div class="welcome-section">
          <div class="logo">
            <img src="@/assets/logo.png" alt="Logo" />
          </div>
          <h1 class="title">三顾茅庐厨师</h1>
          <h2 class="subtitle">小程序后台管理系统</h2>
        </div>
        <form  class="login-form" @submit.prevent="handleLogin">
          <div class="form-group">
            <label for="phone">手机号</label>
            <div class="input-group">
              <i class="fas fa-user"></i>
              <input
                id="phone"
                v-model="phone"
                type="text"
                placeholder="请输入手机号"
                required
              />
            </div>
          </div>

          <div class="form-group">
            <label for="password">密码</label>
            <div class="input-group">
              <i class="fas fa-lock"></i>
              <input
                id="password"
                v-model="password"
                type="password"
                placeholder="请输入密码"
                required
              />
            </div>
          </div>

          <div class="form-options">
            <label class="remember-me">
              <input type="checkbox" v-model="rememberMe" />
              <span>记住我</span>
            </label>
            <a href="#" class="forgot-password">忘记密码？</a>
          </div>

          <button type="submit" class="btn btn-login">
            <span>登录</span>
            <i class="fas fa-arrow-right"></i>
          </button>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { useUserStore } from '../stores/user'
import { useRouter } from 'vue-router'
import * as THREE from 'three'

const userStore = useUserStore()
const router = useRouter()
const phone = ref('15309177684')
const password = ref('123456')
const rememberMe = ref(false)
const canvas = ref(null)

let scene, camera, renderer, particles, raycaster, mouse
let particlePositions, particleColors
let lines, linePositions, lineColors
const particleCount = 150
const maxDistance = 100
const minDistance = 50

const initThree = () => {
  scene = new THREE.Scene()

  camera = new THREE.PerspectiveCamera(
    50,
    window.innerWidth / window.innerHeight,
    1,
    3000
  )
  camera.position.z = 750

  renderer = new THREE.WebGLRenderer({
    canvas: canvas.value,
    alpha: true,
    antialias: true
  })
  renderer.setSize(window.innerWidth, window.innerHeight)
  renderer.setClearColor(0x000000, 0)

  // 创建粒子
  const geometry = new THREE.BufferGeometry()
  particlePositions = new Float32Array(particleCount * 3)
  particleColors = new Float32Array(particleCount * 3)

  for (let i = 0; i < particleCount; i++) {
    const i3 = i * 3
    particlePositions[i3] = Math.random() * 1000 - 500
    particlePositions[i3 + 1] = Math.random() * 1000 - 500
    particlePositions[i3 + 2] = Math.random() * 1000 - 500

    // 为每个粒子设置渐变色
    const hue = i / particleCount
    const color = new THREE.Color().setHSL(hue, 1, 0.5)
    particleColors[i3] = color.r
    particleColors[i3 + 1] = color.g
    particleColors[i3 + 2] = color.b
  }

  geometry.setAttribute('position', new THREE.BufferAttribute(particlePositions, 3))
  geometry.setAttribute('color', new THREE.BufferAttribute(particleColors, 3))

  const material = new THREE.PointsMaterial({
    size: 4,
    vertexColors: true,
    transparent: true,
    opacity: 0.8,
    blending: THREE.AdditiveBlending
  })

  particles = new THREE.Points(geometry, material)
  scene.add(particles)

  // 创建连线
  const lineGeometry = new THREE.BufferGeometry()
  const lineCount = particleCount * particleCount
  linePositions = new Float32Array(lineCount * 6)
  lineColors = new Float32Array(lineCount * 6)
  lineGeometry.setAttribute('position', new THREE.BufferAttribute(linePositions, 3))
  lineGeometry.setAttribute('color', new THREE.BufferAttribute(lineColors, 3))

  const lineMaterial = new THREE.LineBasicMaterial({
    vertexColors: true,
    transparent: true,
    opacity: 0.2,
    blending: THREE.AdditiveBlending
  })

  lines = new THREE.LineSegments(lineGeometry, lineMaterial)
  scene.add(lines)

  // 鼠标交互
  raycaster = new THREE.Raycaster()
  mouse = new THREE.Vector2()

  const onMouseMove = (event) => {
    mouse.x = (event.clientX / window.innerWidth) * 2 - 1
    mouse.y = -(event.clientY / window.innerHeight) * 2 + 1
  }

  window.addEventListener('mousemove', onMouseMove)

  // 动画循环
  let time = 0
  const animate = () => {
    requestAnimationFrame(animate)
    time += 0.001

    // 更新粒子位置
    for (let i = 0; i < particleCount; i++) {
      const i3 = i * 3
      particlePositions[i3 + 1] += Math.sin(time + i) * 0.3
      particlePositions[i3] += Math.cos(time + i) * 0.3
      particlePositions[i3 + 2] += Math.sin(time + i) * 0.3
    }
    particles.geometry.attributes.position.needsUpdate = true

    // 更新连线
    let lineIndex = 0
    for (let i = 0; i < particleCount; i++) {
      for (let j = i + 1; j < particleCount; j++) {
        const dx = particlePositions[i * 3] - particlePositions[j * 3]
        const dy = particlePositions[i * 3 + 1] - particlePositions[j * 3 + 1]
        const dz = particlePositions[i * 3 + 2] - particlePositions[j * 3 + 2]
        const dist = Math.sqrt(dx * dx + dy * dy + dz * dz)

        if (dist < maxDistance && dist > minDistance) {
          const alpha = 1 - dist / maxDistance

          // 第一个点
          linePositions[lineIndex * 6] = particlePositions[i * 3]
          linePositions[lineIndex * 6 + 1] = particlePositions[i * 3 + 1]
          linePositions[lineIndex * 6 + 2] = particlePositions[i * 3 + 2]

          // 第二个点
          linePositions[lineIndex * 6 + 3] = particlePositions[j * 3]
          linePositions[lineIndex * 6 + 4] = particlePositions[j * 3 + 1]
          linePositions[lineIndex * 6 + 5] = particlePositions[j * 3 + 2]

          // 设置线条颜色
          const color1 = new THREE.Color().setHSL(i / particleCount, 1, 0.5)
          const color2 = new THREE.Color().setHSL(j / particleCount, 1, 0.5)

          lineColors[lineIndex * 6] = color1.r
          lineColors[lineIndex * 6 + 1] = color1.g
          lineColors[lineIndex * 6 + 2] = color1.b

          lineColors[lineIndex * 6 + 3] = color2.r
          lineColors[lineIndex * 6 + 4] = color2.g
          lineColors[lineIndex * 6 + 5] = color2.b

          lineIndex++
        }
      }
    }

    lines.geometry.attributes.position.needsUpdate = true
    lines.geometry.attributes.color.needsUpdate = true

    // 鼠标交互效果
    raycaster.setFromCamera(mouse, camera)
    const intersects = raycaster.intersectObject(particles)

    if (intersects.length > 0) {
      const index = intersects[0].index
      particlePositions[index * 3 + 1] += (Math.random() - 0.5) * 2
    }

    // 旋转整个场景
    scene.rotation.y += 0.0005
    scene.rotation.x += 0.0002

    renderer.render(scene, camera)
  }

  animate()

  // 窗口大小调整
  const handleResize = () => {
    camera.aspect = window.innerWidth / window.innerHeight
    camera.updateProjectionMatrix()
    renderer.setSize(window.innerWidth, window.innerHeight)
  }

  window.addEventListener('resize', handleResize)

  return () => {
    window.removeEventListener('resize', handleResize)
    window.removeEventListener('mousemove', onMouseMove)
  }
}

onMounted(() => {
  const cleanup = initThree()
  onBeforeUnmount(cleanup)
})

const handleLogin = async () => {
  if (phone.value) {
    userStore.setUser(phone.value)
    await userStore.login({
      phone:phone.value,
      password:password.value,
    })

    // 重置表单
    phone.value = ''
    password.value = ''
    // 跳转到仪表盘页面
    router.push('/dashboard')
  }
}

const handleLogout = () => {
  userStore.logout()
  // 清除 localStorage 中的用户信息
  localStorage.removeItem('user')
  // 跳转到登录页
  router.push('/login')
}
</script>

<style scoped>
.home {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #ff9966 0%, #ff5e62 100%);
  padding: 20px;
  position: relative;
  overflow: hidden;
}

.particles-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.login-container {
  width: 100%;
  max-width: 420px;
  margin: 0 auto;
  position: relative;
  z-index: 2;
  pointer-events: auto;
}

.login-box {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  padding: 40px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(10px);
}

.welcome-section {
  text-align: center;
  margin-bottom: 30px;
}

.logo {
  margin-bottom: 20px;
}

.logo img {
  width: 80px;
  height: 80px;
  border-radius: 50%;
}

.title {
  color: #2d3748;
  font-size: 2em;
  margin: 0;
  font-weight: 700;
}

.subtitle {
  color: #ff5e62;
  margin-top: 8px;
  font-size: 1.2em;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #4a5568;
  font-weight: 500;
}

.input-group {
  position: relative;
  display: flex;
  align-items: center;
}

.input-group i {
  position: absolute;
  left: 12px;
  color: #a0aec0;
}

.input-group input {
  width: 100%;
  padding: 12px 12px 12px 40px;
  border: 2px solid #e2e8f0;
  border-radius: 10px;
  font-size: 1em;
  transition: all 0.3s ease;
}

.input-group input:focus {
  border-color: #667eea;
  outline: none;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #4a5568;
}

.forgot-password {
  color: #667eea;
  text-decoration: none;
  font-weight: 500;
}

.forgot-password:hover {
  text-decoration: underline;
}

.btn {
  width: 100%;
  padding: 12px 24px;
  border: none;
  border-radius: 10px;
  font-size: 1em;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.btn-login {
  background: #ff5e62;
  color: white;
}

.btn-login:hover {
  background: #ff9966;
  transform: translateY(-2px);
}

.btn-logout {
  background: #fc8181;
  color: white;
}

.btn-logout:hover {
  background: #f56565;
}

.user-panel {
  text-align: center;
}

.avatar {
  width: 80px;
  height: 80px;
  background: #667eea;
  border-radius: 50%;
  margin: 0 auto 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 2em;
  font-weight: bold;
}

.welcome-user {
  color: #2d3748;
  margin: 0 0 8px;
}

.user-status {
  color: #48bb78;
  margin-bottom: 20px;
  font-weight: 500;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-box {
  animation: fadeIn 0.6s ease-out;
}
</style>
